<template>
  <div class="commentsList">
    <el-button type="primary" icon="el-icon-plus" @click.stop="editComment('','add')">添加评论
    </el-button>
    <el-row :gutter="20">
      <template v-for="item in list">
        <el-col :span="21">

          <div class="comment-li bor" v-if="item.status == 0" @click.stop="editComment(item.id,'edit')">
            <p>{{item.content}}</p>
            <p>{{item.publishTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}')}}</p>
          </div>

          <div class="comment-li" v-if="item.status == 1" @click.stop="aaa">
            <el-input type="textarea" v-model="item.content" ></el-input>
            <el-date-picker
              v-model="item.publishTime"
              type="datetime"
              value-format="timestamp"
              placeholder="选择日期时间">
            </el-date-picker>
          </div>

        </el-col>
        <el-col :span="3">
          <div class="comment-li">
            <el-button size="small" type="danger" @click.stop="editComment(item.id,'delete')">删除
            </el-button>
          </div>

        </el-col>
      </template>
    </el-row>

  </div>
</template>

<script>
  export default {
    name: "commentsList",
    props: ['list'],

    methods: {
      aaa(e){

      },
      sortByKey(array,key) {
        return array.sort(function(a,b){
          var x = a[key];
          var y = b[key];
          return((x<y)?-1:((x>y)?1:0));
        })
      },

      saveAll() {
        for (let i = 0; i < this.list.length; i++) {
          this.list[i].status = 0
        }
        this.$emit("changeCommentList",this.list)
      },
      editComment(id, type) {
        if (type == 'add') {
          this.list.unshift({id: new Date().getTime(), content: '', status: 1})
        } else {
          for (let i = 0; i < this.list.length; i++) {

            if (this.list[i].id == id) {

              if (type == 'edit') {
                this.list[i].status = 1
              }
              if (type == 'ok') {
                this.list[i].status = 0
              }
              if (type == 'delete') {
                this.list.splice(i, 1)
              }
            }
          }
        }
        this.$emit("changeCommentList",this.list)
      }
    }
  }
</script>

<style lang="scss" scoped>
  .comment-li {
    margin: 10px 0px;
    cursor: pointer;
    padding:5px;
    p{
      margin:0;
    }
  }
  .bor:hover {
    border:1px dashed #c0c0c0;
  }
</style>
